package com.tuo51.evaluate.repositories.appusers;

import com.tuo51.evaluate.domain.appusers.StaffRedeemCode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface StaffRedeemCodeRepository extends JpaRepository<StaffRedeemCode, String>, JpaSpecificationExecutor<StaffRedeemCode> {

    StaffRedeemCode getByStaffIdAndRedeemCodeId(String staffId, String redeemCodeId);

    Page<StaffRedeemCode> findAllByRedeemCodeIdOrderByCreatedDesc(String redeemCodeId, Pageable pageable);

    Page<StaffRedeemCode> findAllByProductIdOrderByCreatedDesc(String productId, Pageable pageable);

    Page<StaffRedeemCode> findAllByStaffIdOrderByCreatedDesc(String staffId, Pageable pageable);

    @Query(value = "SELECT r.* from staff_redeem_code r LEFT JOIN `redeem_code` c on c.`id` = r.`redeem_code_id` " +
            "WHERE c.`code` LIKE concat('%', ?1, '%') or c.`dealer` LIKE concat('%', ?1, '%')" +
            "ORDER BY ?#{#pageable}", nativeQuery = true)
    List<StaffRedeemCode> findAllByRedeemCodeLikeOrderByCreatedDesc(String searchBy, Pageable pageable);

    @Query(value = "SELECT count(r.id) from staff_redeem_code r LEFT JOIN `redeem_code` c on c.`id` = r.`redeem_code_id` " +
            "WHERE c.`code` LIKE concat('%', ?1, '%') or c.`dealer` LIKE concat('%', ?1, '%')", nativeQuery = true)
    long countAllByRedeemCodeLikeOrderByCreatedDesc(String searchBy);


    Page<StaffRedeemCode> findAllByOrderByCreatedDesc(Pageable pageable);


}
